package com.oa.official.controller;

import com.oa.official.entity.wechat.WechatMenu;
import com.oa.official.response.Result;
import com.oa.official.service.WechatMenuService;
import com.oa.official.vo.wechat.WechatMenuVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "微信菜单")
@RestController
@RequestMapping("/admin/wechat/wechatMenu")
@CrossOrigin
public class WechatMenuController {
    @Resource
    private WechatMenuService wechatMenuService;
    //@PreAuthorize("hasAuthority('bnt.menu.list')")
    @ApiOperation(value = "获取全部菜单-树形结构")
    @GetMapping("/findMenuInfo")
    public Result findMenuInfo() {
        List<WechatMenuVo> menuList = wechatMenuService.findMenuInfo();
        return Result.success(menuList);
    }

    //@PreAuthorize("hasAuthority('bnt.menu.add')")
    @ApiOperation(value = "新增微信公众号菜单")
    @PostMapping("/save")
    public Result save(@RequestBody WechatMenu wechatMenu) {
        wechatMenuService.save(wechatMenu);
        return Result.success();
    }

    //@PreAuthorize("hasAuthority('bnt.menu.update')")
    @ApiOperation(value = "修改微信公众号菜单")
    @PutMapping("/update")
    public Result updateById(@RequestBody WechatMenu wechatMenu) {
        wechatMenuService.updateById(wechatMenu);
        return Result.success();
    }

    //@PreAuthorize("hasAuthority('bnt.menu.remove')")
    @ApiOperation(value = "删除微信公众号菜单")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Long id) {
        wechatMenuService.removeById(id);
        return Result.success();
    }

//______________________________注意:以下接口是为了推送给微信公众号同步数据使用的________________________________________//

    @ApiOperation(value = "同步菜单,将本地自己的菜单推送给微信")
    @GetMapping("/syncMenu")
    public Result syncMenu() {
        wechatMenuService.syncMenu();
        return Result.success(200,null,"同步菜单成功");
    }

    @ApiOperation(value = "删除同步菜单，将微信菜单删除")
    @DeleteMapping("/removeMenu")
    public Result removeMenu() throws WxErrorException {
        wechatMenuService.removeMenu();
        return Result.success(200,null,"菜单删除成功");
    }

    //@PreAuthorize("hasAuthority('bnt.menu.list')")
    @ApiOperation(value = "获取")
    @GetMapping("/get/{id}")
    public Result get(@PathVariable Long id) {
        WechatMenu wechatMenu = wechatMenuService.getById(id);
        return Result.success(wechatMenu);
    }
}
